package cn.shop.dys.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import cn.shop.dys.R;
import cn.shop.dys.activity.ProductActivity;
import cn.shop.dys.bean.Product;
import cn.shop.dys.constant.CommonConstant;
import cn.shop.lib_common.CommonUtil;

/**
 * Created by ZhangJun 2016/4/23 16:08
 */
public class NewProductApdapter2 extends RecyclerView.Adapter<NewProductApdapter2.MyViewHolder> {
    private Context mContext;
    private List<Product> mProducts;

    public NewProductApdapter2(Context context, List<Product> product) {
        this.mContext = context;
        this.mProducts = product;
    }

    public NewProductApdapter2() {

    }

    @Override
    public NewProductApdapter2.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 创建viewholder
        MyViewHolder holder = new MyViewHolder(View.inflate(parent.getContext(), R.layout.view_newproduct_item, null));
        return holder;
    }

    @Override
    public int getItemCount() {
        return mProducts == null ? 0 : mProducts.size();
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        MyViewHolder viewHolder = (MyViewHolder) holder;
        viewHolder.setPosition(position);
        viewHolder.initView();


        // viewHolder.ivPic.setOnClickListener(this);

    }

//    @Override
//    public void onClick(View v) {
//        CommonUtil.showSafeToast(mContext,"去商品详情");
//        Intent intent=new Intent(mContext,ProductActivity.class);
//        intent.putExtra("pId","");
//        mContext.startActivity(intent);
//    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public ImageView ivPic;
        public TextView tVName;
        public TextView tVPrice;
        public int pos;
        private Product product;

        public MyViewHolder(View itemView) {
            super(itemView);
            ivPic = (ImageView) itemView.findViewById(R.id.iv_product);
            tVName = (TextView) itemView.findViewById(R.id.tv_product_title);
            tVPrice = (TextView) itemView.findViewById(R.id.tv_price);

        }

        public void setPosition(int position) {
            this.pos = position;
            product = mProducts.get(position);
        }

        public void initView() {
            this.tVName.setText(product.name);
            this.tVPrice.setText("￥" + product.price);
            String picPortUrl = product.pic;
            String picUrl = CommonConstant.HOST + picPortUrl;
            Log.d("推荐品牌adapter", picUrl);
            Glide.with(mContext).load(picUrl).into(this.ivPic);
            this.ivPic.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            CommonUtil.showSafeToast(mContext, "去商品详情");
            Intent intent = new Intent(mContext, ProductActivity.class);
            intent.putExtra(CommonConstant.PRODUCT_ID_KEY, product.id);
            mContext.startActivity(intent);
        }
    }
}

